moddbfandomcom-20200213-history
MD2
MD2 is the file format used by Quake II for its 3D models. The format supports frame-based vertex animation and is easy to use once you understand the quirks of the encoding. However, before you dive in and commit to using MD2 for your game, be warned, MD2 stores the vertex information as single bytes. This results in a smaller file but loses accuracy in the model geometry. The loss of vertex precision is noticeable when animating the model, the shape wobbles as it moves. Therefore the format isn't the best if your model is going to be close to the viewer for long periods. File Format Information Header The MD2 header consists of 17 integer values: Offset Name Desc 0 ID Always 0x32504449 ('IDP2') 4 Version Always 8 8 TexWidth Texture width in pixels, normally 256 12 TexHeight Texture height in pixels, normally 256 16 FrameSize The size of each frame in bytes 20 nTextures The number of textures 24 nVertices The number of vertices in each frame 28 nTexCoords The number of UV coordinates in the model 32 nFaces The number of triangles in the model 36 nGLCommands The number of GL instructions (4 bytes each) in the GLCmd list 40 nFrames The number of animation frames in the file (All the following offsets are in bytes from the start of the file) 44 TexOffset An offset to the texture name(s) 48 UVOffset An offset to the UV coordinates 52 FaceOffset An offset to the face data 56 FrameOffset An offset to the first animation frame 60 GLCmdOffset An offset to the GLCmd list 64 EOFOffset An offset to the end of the file Texture Names Texture names are stored as 64 byte strings, the path is relative to the Quake II game directory. These 'skins' are normally PCX files. Texture Coordinates Texture coordinates are stored as pairs of short integers. In order to use them with OpenGL, you should divide the first by the texture width and the second by the texture height. This will yield the more usual UV coordinates in the range 0.0f - 1.0f. typedef struct { short U; // Divide by TexWidth short V; // Divide by TexHeight }MD2UVCoord; Face Data MD2 only supports triangles for face data, these are stored as references into the vertex and UV arrays. Each triangle constists of six short integers, the first three are the vertex indices, the second three are the UV coordinate indices. The following structure may be useful in extracting these values: typedef struct { short Point3; short UV3; }MD2Face; Vertex and Animation Frame Data Here's the tricky part. MD2 animation works by providing complete vertex arrays for each frame, while the face offsets into the vertex and UV arrays remain the same. Frames consist of a header which provides a scale and offset value which must be applied to each vertex value in order to convert it into object coordinates. The header looks like this: typedef struct { float Scale3; float Translate3; char Name16; }MD2FrameHeader; The Name field is the name of the frame, these normally follow the form "Walk001","Walk002",etc. The vertex data follows the header, each vertex has the following format: typedef struct { byte Vtx3; byte Normal; }MD2Vtx; The Normal value is an index into a lookup table of normals held by Quake II. This can be disregarded as normals can be calculated once the triangle data has been loaded. The vertices are compressed, in order to uncompress them for rendering you need to apply the scale and translation vectors from the frame header: vertex0 = (frame.Scale0 * Vtx0) + frame.Translate0; vertex1 = (frame.Scale1 * Vtx1) + frame.Translate1; vertex2 = (frame.Scale2 * Vtx2) + frame.Translate2; References * MD2 Model Loader example by Saad Faisal Category:Fileformat